<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>H+ 后台主题UI框架 - 基础表格</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="../favicon.ico"> <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="../css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">
    <link href="../css/shop/style.css" rel="stylesheet">
    <link rel="stylesheet" href="../css/global.css">
</head>

<body class="gray-bg">
<div>
    <div class="row" style="padding:30px;">

        <div class="col-sm-12 animated fadeInRight" style="background: #fff;padding:30px 10px;">
            <div class="row">
                <div class="col-sm-2 m-b-xs">
                    <!--<select class="input-sm form-control input-s-sm inline" style="font-size: 12px">-->
                    <!--<option value="0">排序关键字</option>-->
                    <!--<option value="1">注册时间</option>-->
                    <!--<option value="2">订单</option>-->
                    <!--<option value="3">会员等级</option>-->
                    <!--</select>-->
                    <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">添加</button>
                </div>
                <div class="col-sm-2 m-b-xs">
                    <!--<select class="input-sm form-control input-s-sm inline" style="font-size: 12px">-->
                    <!--<option value="0">正序</option>-->
                    <!--<option value="1">倒序</option>-->
                    <!--</select>-->
                </div>
                <div class="col-sm-3 m-b-xs">
                    <!--<div data-toggle="buttons" class="btn-group">-->
                    <!--<label class="btn btn-sm btn-white">-->
                    <!--<input type="radio" id="option1" name="options">天</label>-->
                    <!--<label class="btn btn-sm btn-white active">-->
                    <!--<input type="radio" id="option2" name="options">周</label>-->
                    <!--<label class="btn btn-sm btn-white">-->
                    <!--<input type="radio" id="option3" name="options">月</label>-->
                    <!--</div>-->
                </div>
                <div class="col-sm-2">
                    <!--<select class="input-sm form-control input-s-sm inline" style="font-size: 12px" id="">-->
                        <!--<option value="0">选择查询字段</option>-->
                        <!--<option value="1">商品类别</option>-->
                        <!--<option value="2">门店</option>-->
                    <!--</select>-->

                </div>
                <div class="col-sm-3" style="font-size: 12px">
                    <!--<div class="input-group">-->
                    <!--<input type="text" id='searchContent' placeholder="请输入查询内容" class="input-sm form-control place-holder" >-->
                    <!--<span class="input-group-btn">-->
                    <!--<button type="button" class="btn btn-sm btn-primary" id="searchBtn">搜索</button>-->
                    <!--</span>-->
                    <!--</div>-->
                    <!--<select class="input-sm form-control input-s-sm inline" style="font-size: 12px" id="searchKey">-->
                        <!--<option value="0">选择商品类别</option>-->
                        <!--<option value="1">商品类别</option>-->
                        <!--<option value="2">门店</option>-->
                    <!--</select>-->
                </div>
            </div>
            <!--<div class="group-set">-->
            <!--<div>批量处理：</div>-->
            <!--<input type="checkbox"  class="i-checks" name="input[]" id="selectAll">-->
            <!--&lt;!&ndash;<button>删除</button>&ndash;&gt;-->
            <!--<button id="btn">确认发货</button>-->
            <!--<button id="cancleBtn">取消发货</button>-->
            <!--<button id="">确认收货</button>-->
            <!--</div>-->
            <div class="table-responsive" style="position: relative;padding-bottom:50px;">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <!--<th></th>-->
                        <th>图片</th>
                        <th>URL</th>
                        <th>提示</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="banner-list">

                    </tbody>
                </table>
                <!--<div id="page" class="page_div"></div>-->
            </div>

        </div>
    </div>
</div>

<!-- Button trigger modal -->
<!--<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">-->
<!--Launch demo modal-->
<!--</button>-->

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">轮播图</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <input type="hidden" id="id" >
                    <div class="form-group">
                        <label class="col-sm-3 control-label">图片上传</label>
                        <div class="col-sm-3">
                            <!--<input type="email" class="form-control" id="inputClassName" placeholder="请输入商品类别名称">-->
                            <input type="file" accept="image/*" id="bannerSel">
                            <input type="hidden" id="bannerUrl">
                        </div>
                        <div class="col-sm-6" id="banner-img">

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="url" class="col-sm-3 control-label">URL</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="url" placeholder="请输入URL">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label" >提示</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="title" placeholder="请输入提示信息">
                        </div>
                    </div>


                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="submitBtn">提交</button>
            </div>
        </div>
    </div>
</div>

<!-- 全局js -->
<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>

<script src="../js/plugins/jeditable/jquery.jeditable.js"></script>

<!-- Data Tables -->
<script src="../js/plugins/dataTables/jquery.dataTables.js"></script>
<script src="../js/plugins/dataTables/dataTables.bootstrap.js"></script>


<!-- Peity -->
<script src="../js/plugins/peity/jquery.peity.min.js"></script>
<!-- iCheck -->
<script src="../js/plugins/iCheck/icheck.min.js"></script>

<!-- Peity -->
<script src="../js/demo/peity-demo.js"></script>

<!--paging-->
<script src="../js/my-js/paging.js"></script>
<!-- 自定义js -->
<script src="../js/content.js?v=1.0.0"></script>
<script src="../js/my-js/global.js"></script>
<script src="../js/my-js/new-order.js"></script>
<script>
    $(document).ready(function(){
        getAjax(global.apiUrl+'/admin_get_shop_banner',function(res){
            var html = '';
            console.log(res);
            for(var i=0;i<res.length;i++){
                html += `
                    <tr>
                        <td class="banner-img">
                            <img src="${res[i].img}" alt="">
                        </td>
                        <td>
                            ${res[i].url==null?'-':res[i].url}
                        </td>
                        <td>
                            ${res[i].title==null?'-':res[i].title}
                        </td>
                        <td>
                            <button class="btn btn-warning btn-sm del" data-id="${res[i].id}">删除</button>
                        </td>
                    </tr>
                `
            }
            $('#banner-list').html(html);
        })
        $('#submitBtn').on('click',function(){
            var imgUrl = $('#bannerUrl').val();
            var url = $('#url').val();
            var title = $('#title').val();
            console.log(imgUrl,url,title);
            getAjax(global.apiUrl+'/admin_add_shop_banner',function(res){
                console.log(res);
                if(res == 1){
                    alert('提交成功！');
                    location.reload();
                }
            },{img:imgUrl,url:url,title:title})
        });
        $('#banner-list').on('click','.del',function(){
            var id = $(this).data('id');
            var qr = confirm('是否确认删除该图片？');
            if(qr){
                console.log(id);
                getAjax(global.apiUrl+'/admin_delete_shop_banner',function(res){
                    if(res == 1){
                        alert('删除成功！');
                        location.reload();
                    }
                },{id:id})
            }
        });
        /*
        * 添加轮播图
        */
        //选择图片

        var bannerSel = document.getElementById('bannerSel');
        var bannerImg = document.getElementById('banner-img');
        bannerSel.addEventListener('change',function(e){
            bannerImg.innerHTML = '';
            //获取file的引用
            var file = bannerSel.files;
            var img = ' ';
            //读取文件
            for(var i=0,file=bannerSel.files;i<file.length;i++){
                var reader = new FileReader();
                reader.onload = function(e){
                    console.log(e);
                    var data = e.target.result;
                    console.log(e.target)
                    // fileImage.style.backgroundImage = 'url('+data+')';
                    var img = document.createElement("img");
                    img.src=data;
                    bannerImg.appendChild(img)
                }
                // 以DataURL的形式读取文件:
                reader.readAsDataURL(file[i]);
            }
            // imgUpload(icon,setIcon)
            imgUpload(bannerSel,setBannerUrl)
        })

        function setBannerUrl(res){
            $('#bannerUrl').val(global.apiUrl + '/'+res)
        }
        function imgUpload(selector,fn){
            if(selector.files.length==0){
                alert('请先选择图片再点击上传')
            }else{
                for(var i=0;i<selector.files.length;i++){
                    uploadImg(selector.files[i],fn);
                }
            }
        }
        function uploadImg(f,fn) {
            var xhr = new XMLHttpRequest();
            var formData = new FormData();
            // for(var i=0; f=fileInput.files[i]; i++){
            //     formData.append('files', f);
            // }
            formData.append('file',f);
            xhr.onreadystatechange = function (e) {
                if(xhr.readyState == 4){
                    if(xhr.status == 200){
                        console.log(xhr.responseText);
                        fn(xhr.responseText)
                    }else {
                        console.log(xhr.responseText);
                    }
                }
            }

            xhr.open('POST', global.apiUrl + '/admin_upload', true);
            xhr.send(formData);
        }

    })


</script>






</body>

</html>
